package com.shigu.hotItem.service;

import com.opentae.data.mall.beans.MallHotItem;
import com.opentae.data.mall.beans.MallItem;
import com.opentae.data.mall.beans.MallItemcats;
import com.opentae.data.mall.examples.MallHotItemExample;
import com.opentae.data.mall.examples.MallItemExample;
import com.opentae.data.mall.interfaces.MallHotItemMapper;
import com.opentae.data.mall.interfaces.MallItemMapper;
import com.opentae.data.mall.interfaces.MallItemcatsMapper;
import com.shigu.ListDto;
import com.shigu.PageResponse;
import com.shigu.account.api.VipManagerService;
import com.shigu.exceptions.AdminException;
import com.shigu.exceptions.WapException;
import com.shigu.hotItem.api.HotItemService;
import com.shigu.hotItem.bo.HotItemBo;
import com.shigu.hotItem.bo.ModifyHotItemBo;
import com.shigu.hotItem.vo.AdminHotVo;
import com.shigu.hotItem.vo.HotGoodsVo;
import com.shigu.item.enums.ImgRule;
import com.shigu.item.vo.ItemIdResponse;
import com.shigu.util.MoneyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/*
 * create by  过儿  2019-06-04
 *
 */
@Service(value = "hotItemServiceImpl")
public class HotItemServiceImpl implements HotItemService {

    @Autowired
    MallHotItemMapper mallHotItemMapper;

    @Autowired
    MallItemMapper mallItemMapper;

    @Autowired
    MallItemcatsMapper mallItemcatsMapper;

    @Resource(name = "vipManagerServiceImpl")
    VipManagerService vipManagerService;

    private static String tuijian = "推荐";
    private static String rootCate = "-1";

    @Override
    public void addHotItem(HotItemBo bo) throws WapException {

        MallItem item = mallItemMapper.selectByPrimaryKey(bo.getItemId());
        if(item==null){
            throw new WapException("不存在此商品");
        }

        long cid = bo.getCid()==null?-1L:bo.getCid();

        MallHotItemExample example = new MallHotItemExample();
        example.createCriteria().andItemIdEqualTo(bo.getItemId()).andCidEqualTo(cid);
        if (mallHotItemMapper.countByExample(example) > 0) {
            //已存在
            throw new WapException("该类目下已添加该商品");
        }

        MallHotItem hotItem = new MallHotItem();
        hotItem.setItemId(bo.getItemId());

        if(StringUtils.isNotBlank(bo.getImgUrl())){
            hotItem.setImgUrl(bo.getImgUrl());
        }else {
            hotItem.setImgUrl(item.getAdvertImg());
        }

        hotItem.setCreateTime(new Date());
        hotItem.setShowWeight(bo.getWeight());
        //默认推荐
        hotItem.setCid(cid);
        mallHotItemMapper.insertSelective(hotItem);
    }

    @Override
    public void delHotItem(Long hotId) {
        mallHotItemMapper.deleteByPrimaryKey(hotId);
    }

    @Override
    public void changeShowWeight(Long hotId, int weight) {
        MallHotItem hotItem = new MallHotItem();
        hotItem.setHotId(hotId);
        hotItem.setShowWeight(weight);
        mallHotItemMapper.updateByPrimaryKeySelective(hotItem);
    }

    @Override
    public void modifyHotItem(ModifyHotItemBo bo) {
        MallHotItem hotItem = new MallHotItem();
        hotItem.setHotId(bo.getHotId());
        hotItem.setCid(bo.getCid());
        hotItem.setShowWeight(bo.getWeight());
        hotItem.setImgUrl(bo.getImgUrl());
        hotItem.setShowWeight(bo.getWeight());
        mallHotItemMapper.updateByPrimaryKeySelective(hotItem);
    }


    @Override
    public ListDto<MallHotItem> selHotItemList(Integer page, Integer size, String cid,Long itemId) {
        MallHotItemExample example = new MallHotItemExample();
        example.setStartIndex((page - 1) * size);
        example.setEndIndex(size);
        example.setOrderByClause("show_weight desc");
        MallHotItemExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(cid)) {
            criteria.andCidEqualTo(Long.valueOf(cid));
        }
        if(itemId!=null){
            criteria.andItemIdEqualTo(itemId);
        }

        int total = mallHotItemMapper.countByExample(example);
        List<MallHotItem> list = mallHotItemMapper.selectByConditionList(example);

        return new ListDto<>(total > page * size, total, list);
    }

    @Override
    public PageResponse<HotGoodsVo> getHotGoodsList(Integer page, Integer size, String cid, Long mallUserId) {
        cid = StringUtils.isBlank(cid)? rootCate:cid;
        //查询用户分享的费率
        final Double rate = vipManagerService.userRate(mallUserId,"");

        ListDto<MallHotItem> dto = selHotItemList(page, size, cid,null);
        PageResponse<HotGoodsVo> response = new PageResponse<>();
        response.setTotal(dto.getTotal());
        response.setHasNext(dto.getHasNext());
        response.setData(dto.getData().stream().map(o -> {
            MallItem item = mallItemMapper.selectByPrimaryKey(o.getItemId());
            HotGoodsVo vo = new HotGoodsVo();
            String[] imgs = o.getImgUrl().split(",");
            //图片加上图片规则
            for (int i = 0; i <imgs.length ; i++) {
                if( !imgs[i].endsWith(ImgRule.rule1.getText())){
                    imgs[i]+= ImgRule.rule1.getText();
                }
            }
            vo.setImgs(Arrays.asList(imgs));
            vo.setItemId(o.getItemId());
            vo.setLiPrice(MoneyUtil.formatPriceMoney(item.getPrice()));
            vo.setPrice(MoneyUtil.formatPriceMoney(item.getDiscount()));
            vo.setTitle(item.getTitle());
            vo.setCommission(MoneyUtil.formatPriceMoney(item.getDiscount() * rate));
            return vo;
        }).collect(Collectors.toList()));

        return response.success();
    }

    @Override
    public PageResponse<AdminHotVo> hotItemList(Integer page, Integer size, String cid,String itemCode) {
        Long itemId = codeToId(itemCode);
        ListDto<MallHotItem> dto = selHotItemList(page, size, cid,itemId);
        PageResponse<AdminHotVo> response = new PageResponse<>();
        response.setData(dto.getData().stream().map(o -> {
            MallItem item = mallItemMapper.selectByPrimaryKey(o.getItemId());
            MallItemcats cate = mallItemcatsMapper.selectByPrimaryKey(o.getCid());
            AdminHotVo vo = new AdminHotVo();
            vo.setItemId(o.getItemId());
            vo.setImgs(Arrays.asList(o.getImgUrl().split(",")));
            vo.setItemCode(item.getItemCode());
            vo.setCateName(cate == null ? tuijian : cate.getCatName());
            vo.setCid(o.getCid() + "");
            vo.setItemCode(item.getItemCode());
            vo.setHotId(o.getHotId());
            vo.setTitle(item.getTitle());
            vo.setWeight(o.getShowWeight());
            return vo;
        }).collect(Collectors.toList()));

        response.setTotal(dto.getTotal());
        return response.success();
    }

    private Long codeToId(String itemCode){
        MallItemExample example = new MallItemExample();
        example.createCriteria().andItemCodeEqualTo(itemCode);
        List<MallItem> items = mallItemMapper.selectByExample(example);
        if(items.size()==0){
            return null;
        }
        return items.get(0).getItemId();
    }
}
